package forum.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;


import forum.shared.Delimiters;
import forum.shared.ServerResponse;

public class AddPostDialog extends Screen {
	final DialogBox dialog = new DialogBox(false, true);
	VerticalPanel vPanel;
	HorizontalPanel hPanel;
	HorizontalPanel buttonsPanel;
	TextBox subjectTxt;;
	TextArea contentTxt;
	final Button submitBtn = new Button("Subimt");
	Button closeBtn;
	Label errorLbl;
	
	private int parentPostId = -1;
	
	/**
	 * Default constructor for new post add.
	 */
	public AddPostDialog() {
		this(-1);
	}

	/**
	 * Constructor for adding a reply.
	 * 
	 * @param parentPostId
	 */
	public AddPostDialog(int parentPostId) {
		init();
		this.parentPostId = parentPostId;
	}

	@Override
	protected void createComponents() {
		vPanel = new VerticalPanel();
		hPanel = new HorizontalPanel();
		buttonsPanel = new HorizontalPanel();
		subjectTxt = new TextBox();
		contentTxt = new TextArea();
		closeBtn = new Button("Close");
		errorLbl = new Label();
//		errorLbl.setVisible(false);
	}

	@Override
	protected String getInput() {
		String d = Delimiters.MSG_PARTS;
		
		if (subjectTxt.getText().isEmpty()) {
			errorLbl.setText("Please fill the subject field.");
			errorLbl.setVisible(true);
			return null;
		}
		
		
		errorLbl.setVisible(false);
		
		if (this.parentPostId == -1)
			return "AddPost" + d + subjectTxt.getText() + d + contentTxt.getText();
		else
			return "AddReply" + d + parentPostId + d + subjectTxt.getText() + d + 
			contentTxt.getText() + d;
	}

	@Override
	protected void setActions() {
		closeBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				dialog.hide(true);
			}
		});
		
		submitBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				submitBtn.setEnabled(false);
				ForumServiceAsync forumSvc = GWT.create(ForumService.class);
				
				// Set up the callback object.
			    AsyncCallback<ServerResponse> callback = new AsyncCallback<ServerResponse>() {
			    	public void onFailure(Throwable caught) {
			    		errorLbl.setText("Communication error. Please retry later.");
			    		errorLbl.setVisible(true);
			    		submitBtn.setEnabled(true);
			    	}
			    	//On successful communication.
					@Override
					public void onSuccess(ServerResponse response) {
						if (response!=null && response.hasExecuted()) {
							dialog.hide(true);
							new MainScreen().setupDisplayInMainPanel();
			            } 
						else {
							errorLbl.setText(response!=null ? 
									response.getResponse() : "Unknown Error...");
							errorLbl.setVisible(true);
			            }
						submitBtn.setEnabled(true);
					}
			    };
			    
				// Make the call to the forum service.
			    String input = getInput();
			    if (input != null)
			    	forumSvc.sendMsg(input, callback);
			    else
			    	submitBtn.setEnabled(true);
			}
		});
	}

	@Override
	protected void setStyle() {
		vPanel.addStyleName("dialogBox");
		submitBtn.addStyleName("button");
		closeBtn.addStyleName("button");
		dialog.addStyleName("dialogBoxHeader");
		vPanel.setSpacing(10);
		errorLbl.addStyleName("errorLabel");
	}

	@Override
	protected void setTabOrder() {
		
	}

	@Override
	public void setupDisplayInMainPanel() {
		dialog.center();
	}

	@Override
	protected void setupScreenDisplay() {
		hPanel.add(new Label("Subject: "));
		hPanel.add(subjectTxt);
		vPanel.add(hPanel);
		vPanel.add(contentTxt);
		vPanel.add(errorLbl);
		buttonsPanel.add(submitBtn);
		buttonsPanel.add(closeBtn);
		vPanel.add(buttonsPanel);
		
		dialog.setText(parentPostId==-1 ? "Add new post" : "Add reply");
		dialog.setWidget(vPanel);
	}

}
